package cardmanager;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

public class CardManager {
	
	static Hashtable<Integer,CardInfo> m_cards = new Hashtable<Integer,CardInfo>();
	static boolean isLoaded = false;

	public static boolean getIsLoaded(){
		return isLoaded;
	}
	public static boolean load(String dir){
		
		if(isLoaded){
			System.out.println("Card database is already loaded.");
			return false;
		}
		
		if(!new File(dir).exists()){
			System.out.println("Database file not found.");
			return false;
		}
		
		try {
			Class.forName("org.sqlite.JDBC");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return false;
		}

	    Connection connection = null;
	    try
	    {
	      // create a database connection
	      connection = DriverManager.getConnection("jdbc:sqlite:" +dir);
	      Statement statement = connection.createStatement();
	      statement.setQueryTimeout(30);  // set timeout to 30 sec.

	      ResultSet rs = statement.executeQuery("SELECT id, alias, type, level, race, attribute, atk, def FROM datas");
	      
	      while(rs.next()){
	    	  int id = rs.getInt(1);
	    	  m_cards.put(id, new CardInfo(id, rs.getInt(2), rs.getInt(7), rs.getInt(8), rs.getInt(6),
	    			  rs.getInt(4),rs.getInt(5), rs.getInt(3)));
	      }
	      
	      rs = statement.executeQuery("SELECT id, name, desc FROM texts");
	      while(rs.next()){
	    	  int id = rs.getInt(1);
	    	  if(m_cards.containsKey(id)){
	    		  CardInfo card = m_cards.get(id);
	    		  card.setText(rs.getString(2), rs.getString(3));
	    	  }
	      }
	    }
	    catch(SQLException e){
	      System.err.println(e.getMessage());
	      return false;
	    }
	    finally{
	      try{
	        if(connection != null)
	          connection.close();
	      }
	      catch(SQLException e){
	        System.err.println(e);
	      }
	    }
	    isLoaded = true;
	    System.out.println("Loaded " + m_cards.size() + " cards.");
	    return true;
	}
	
    public static CardInfo fromId(int id)
    {
        if (m_cards.containsKey(id))
        {
            return m_cards.get(id);
        }
        return null;
    }
    
    public static ArrayList<CardInfo> search(String text)
    {
    	if(text == null || text == "") return null;
        text = text.trim().toLowerCase();
        ArrayList<CardInfo> list = new ArrayList<CardInfo>();
        
        Enumeration<Integer> enumKey = m_cards.keys();
        while(enumKey.hasMoreElements()) {
            Integer key = enumKey.nextElement();
            CardInfo val = m_cards.get(key);
            if (val.getName().contains(text)){
                list.add(val);
            }
        }
        return list;
    }
}
